//
//  NIVehicleStatusView.m
//  gweAD
//
//  Created by navinfo air on 2017/2/23.
//  Copyright © 2017年 mengy. All rights reserved.
//

#import "NIVehicleStatusView.h"

@implementation NIVehicleStatusView
{
    __weak IBOutlet UIView *contentView;
    
    __weak IBOutlet UIImageView *controlCarImageView;
    __weak IBOutlet UIImageView *noSkyWindowImageView;
    __weak IBOutlet UIImageView *window1AnimateImageView;
    __weak IBOutlet UIImageView *window2AnimateImageView;
    __weak IBOutlet UIImageView *window3AnimateImageView;
    __weak IBOutlet UIImageView *window4AnimateImageView;
    
    __weak IBOutlet UIImageView *window1ForDoorAnimateImageView;
    __weak IBOutlet UIImageView *window2ForDoorAnimateImageView;
    __weak IBOutlet UIImageView *window3ForDoorAnimateImageView;
    __weak IBOutlet UIImageView *window4ForDoorAnimateImageView;
    
    __weak IBOutlet UIImageView *skyWindowAnimateImageView;
    
    NSInteger animationTimes;
    
    
    __weak IBOutlet UIImageView *trunkImageView;
    __weak IBOutlet UIImageView *trunkInvaildImageView;
    
    __weak IBOutlet UIImageView *window1ImageView;
    __weak IBOutlet UIImageView *window2ImageView;
    __weak IBOutlet UIImageView *window3ImageView;
    __weak IBOutlet UIImageView *window4ImageView;
    __weak IBOutlet UIImageView *skyWindowImageView;
    
    __weak IBOutlet UIImageView *window1InvaildImageView;
    __weak IBOutlet UIImageView *window2InvaildImageView;
    __weak IBOutlet UIImageView *window3InvaildImageView;
    __weak IBOutlet UIImageView *window4InvaildImageView;
    __weak IBOutlet UIImageView *skyWindowInvaildImageView;
    
    __weak IBOutlet UIImageView *door1ImageView;
    __weak IBOutlet UIImageView *door2ImageView;
    __weak IBOutlet UIImageView *door3ImageView;
    __weak IBOutlet UIImageView *door4ImageView;
    
    __weak IBOutlet UIImageView *door1InvaildImageView;
    __weak IBOutlet UIImageView *door2InvaildImageView;
    __weak IBOutlet UIImageView *door3InvaildImageView;
    __weak IBOutlet UIImageView *door4InvaildImageView;
    
    __weak IBOutlet UIImageView *lightImageView;
    
    __weak IBOutlet UIImageView *lock1ImageView;
    
    __weak IBOutlet UIImageView *lock2ImageView;
    __weak IBOutlet UIImageView *unlock1ImageView;
    
    __weak IBOutlet UIImageView *unlock2ImageView;
    __weak IBOutlet UIImageView *startEngine2ImageView;
    __weak IBOutlet UIImageView *startEngine1ImageView;
    
    __weak IBOutlet UIView *circleAnimationView;
    
    __weak IBOutlet UIImageView *window1ForDoorImageView;
    __weak IBOutlet UIImageView *window2ForDoorImageView;
    __weak IBOutlet UIImageView *window3ForDoorImageView;
    __weak IBOutlet UIImageView *window4ForDoorImageView;
    
    __weak IBOutlet UIImageView *window1InvaildForDoorImageView;
    __weak IBOutlet UIImageView *window2InvaildForDoorImageView;
    __weak IBOutlet UIImageView *window3InvaildForDoorImageView;
    __weak IBOutlet UIImageView *window4InvaildForDoorImageView;
    
    __weak IBOutlet UIImageView *lockImageView;
    __weak IBOutlet UIImageView *honkImageView;
    
    
    CALayer *circleAnimationlayer;
    
    NIVehicleAnimationType runningAnimationType;
    
    BOOL skyWindowSupport;
    NSString *vehicleTypeName;
    
}

- (void)initImageAccessibilityIdentifier
{
        controlCarImageView.accessibilityIdentifier = @"control_car";
        noSkyWindowImageView.accessibilityIdentifier = @"car_nosunroof";
        window1AnimateImageView.accessibilityIdentifier = @"control_car1_window_1";
        window2AnimateImageView.accessibilityIdentifier = @"control_car1_window_2";
        window3AnimateImageView.accessibilityIdentifier = @"control_car1_window_3";
        window4AnimateImageView.accessibilityIdentifier = @"control_car1_window_4";
        
        window1ForDoorAnimateImageView.accessibilityIdentifier = @"control_car1_doorwindow_1";
        window2ForDoorAnimateImageView.accessibilityIdentifier = @"control_car1_doorwindow_2";
        window3ForDoorAnimateImageView.accessibilityIdentifier = @"control_car1_doorwindow_3";
        window4ForDoorAnimateImageView.accessibilityIdentifier = @"control_car1_doorwindow_4";
        
        skyWindowAnimateImageView.accessibilityIdentifier = @"control_car1_window_Skylight";
        
        trunkImageView.accessibilityIdentifier = @"Home_car1_trunk";
        trunkInvaildImageView.accessibilityIdentifier = @"Home_TPMS_failure_trunk";
        
        window1ImageView.accessibilityIdentifier = @"Home_car1_window_1";
        window2ImageView.accessibilityIdentifier = @"Home_car1_window_2";
        window3ImageView.accessibilityIdentifier = @"Home_car1_window_3";
        window4ImageView.accessibilityIdentifier = @"Home_car1_window_4";
        skyWindowImageView.accessibilityIdentifier = @"Home_car1_window_Skylight";
        
        window1InvaildImageView.accessibilityIdentifier = @"Home_TPMS_failure_window1";
        window2InvaildImageView.accessibilityIdentifier = @"Home_TPMS_failure_window2";
        window3InvaildImageView.accessibilityIdentifier = @"Home_TPMS_failure_window3";
        window4InvaildImageView.accessibilityIdentifier = @"Home_TPMS_failure_window4";
        skyWindowInvaildImageView.accessibilityIdentifier = @"Home_TPMS_failure_skylight";
        
        door1ImageView.accessibilityIdentifier = @"Home_car1_door1";
        door2ImageView.accessibilityIdentifier = @"Home_car1_door2";
        door3ImageView.accessibilityIdentifier = @"Home_car1_door3";
        door4ImageView.accessibilityIdentifier = @"Home_car1_door4";
        
        door1InvaildImageView.accessibilityIdentifier = @"Home_TPMS_failure_door1";
        door2InvaildImageView.accessibilityIdentifier = @"Home_TPMS_failure_door2";
        door3InvaildImageView.accessibilityIdentifier = @"Home_TPMS_failure_door3";
        door4InvaildImageView.accessibilityIdentifier = @"Home_TPMS_failure_door4";
        
        lightImageView.accessibilityIdentifier = @"control_car_light";
        
        
        
        window1ForDoorImageView.accessibilityIdentifier = @"Home_car1_doorwindow_1";
        window2ForDoorImageView.accessibilityIdentifier = @"Home_car1_doorwindow_2";
        window3ForDoorImageView.accessibilityIdentifier = @"Home_car1_doorwindow_3";
        window4ForDoorImageView.accessibilityIdentifier = @"Home_car1_doorwindow_4";
        
        window1InvaildForDoorImageView.accessibilityIdentifier = @"Home_TPMS_failure_dooropen_window1";
        window2InvaildForDoorImageView.accessibilityIdentifier = @"Home_TPMS_failure_dooropen_window2";
        window3InvaildForDoorImageView.accessibilityIdentifier = @"Home_TPMS_failure_dooropen_window3";
        window4InvaildForDoorImageView.accessibilityIdentifier = @"Home_TPMS_failure_dooropen_window4";
        
//        lockImageView;
//        honkImageView;
}


- (void)setVehicleType:(NSString *)vehicleType{
    if ([vehicleTypeName isEqualToString:vehicleType]) {
        return;
    }
    vehicleTypeName = vehicleType;
    NSString *shortName = @"";
    if ([vehicleType.lowercaseString isEqualToString:@"wey vv5"]) {
//        shortName = @"vv5";
    }else if ([vehicleType.lowercaseString isEqualToString:@"wey p8"]) {
        shortName = @"p8";
    }
    NSString *imageName;
    UIImage *tmpImage;
    for (UIView *view in contentView.subviews) {
        if ([view isKindOfClass:[UIImageView class]]) {
            imageName = ((UIImageView *)view).accessibilityIdentifier;
            if(![shortName isEqualToString:@""]){
                imageName = [NSString stringWithFormat:@"%@_%@",imageName,shortName];
            }
            tmpImage = [UIImage imageNamed:imageName];
            if (tmpImage) {
                [(UIImageView *)view setImage:tmpImage];
            }else{
                NSLog(@"%@",imageName);
            }
        }
    }
}

#pragma mark - reset
- (void)resetStatus{
    //关闭所有动画
    [self stopBaseAnimation];
    [self stopLockUnlockAnimation];
    runningAnimationType = NIVehicleAnimationTypeNone;
    
    [self toBaseStatus];
}

- (void)toBaseStatus{
    noSkyWindowImageView.hidden = YES;
    window1AnimateImageView.hidden = YES;
    window2AnimateImageView.hidden = YES;
    window3AnimateImageView.hidden = YES;
    window4AnimateImageView.hidden = YES;
    
    window1ForDoorAnimateImageView.hidden = YES;
    window2ForDoorAnimateImageView.hidden = YES;
    window3ForDoorAnimateImageView.hidden = YES;
    window4ForDoorAnimateImageView.hidden = YES;
    
    skyWindowAnimateImageView.hidden = YES;
    
    window1ImageView.hidden = YES;
    window2ImageView.hidden = YES;
    window3ImageView.hidden = YES;
    window4ImageView.hidden = YES;
    skyWindowImageView.hidden = YES;
    
    window1InvaildImageView.hidden = YES;
    window2InvaildImageView.hidden = YES;
    window3InvaildImageView.hidden = YES;
    window4InvaildImageView.hidden = YES;
    skyWindowInvaildImageView.hidden = YES;
    
    window1ForDoorImageView.hidden = YES;
    window2ForDoorImageView.hidden = YES;
    window3ForDoorImageView.hidden = YES;
    window4ForDoorImageView.hidden = YES;
    
    window1InvaildForDoorImageView.hidden = YES;
    window2InvaildForDoorImageView.hidden = YES;
    window3InvaildForDoorImageView.hidden = YES;
    window4InvaildForDoorImageView.hidden = YES;
    
    door1ImageView.hidden = YES;
    door2ImageView.hidden = YES;
    door3ImageView.hidden = YES;
    door4ImageView.hidden = YES;
    
    
    door1InvaildImageView.hidden = YES;
    door2InvaildImageView.hidden = YES;
    door3InvaildImageView.hidden = YES;
    door4InvaildImageView.hidden = YES;
    
    trunkImageView.hidden = YES;
    trunkInvaildImageView.hidden = YES;
    
    lightImageView.hidden = YES;
    
    lock1ImageView.hidden = YES;
    lock2ImageView.hidden = YES;
    unlock1ImageView.hidden = YES;
    unlock2ImageView.hidden = YES;
    startEngine1ImageView.hidden = YES;
    startEngine2ImageView.hidden = YES;
    circleAnimationView.hidden = YES;
    
    honkImageView.hidden = YES;
    
    lockImageView.hidden = YES;
}

-(void)awakeFromNib{
    [super awakeFromNib];
    [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:self options:nil];
    _enable = YES;
    [self resetStatus];
    
    [self addSubview:contentView];
    contentView.backgroundColor = [UIColor clearColor];
    contentView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]];
    [self initImageAccessibilityIdentifier];
}

#pragma mark - view&data
- (void)reloadData{
//    [self resetStatus];
//    if (!_enable) {
//        return;
//    }
//    //设置车窗状态
//    //天窗
//    if (!skyWindowSupport) {
//        noSkyWindowImageView.hidden = NO;
//    }else{
//        skyWindowImageView.hidden = [self isClosed:_vehicleInfoModel.sRSts];
//        skyWindowInvaildImageView.hidden = [self isVaild:_vehicleInfoModel.sRSts];
//    }
//
//    window1ImageView.hidden = [self isClosed:_vehicleInfoModel.drvWinSts];
//    window2ImageView.hidden = [self isClosed:_vehicleInfoModel.passWinSts];
//    window3ImageView.hidden = [self isClosed:_vehicleInfoModel.rLWinSts];
//    window4ImageView.hidden = [self isClosed:_vehicleInfoModel.rRWinSts];
//
//
//    window1InvaildImageView.hidden = [self isVaild:_vehicleInfoModel.drvWinSts];
//    window2InvaildImageView.hidden = [self isVaild:_vehicleInfoModel.passWinSts];
//    window3InvaildImageView.hidden = [self isVaild:_vehicleInfoModel.rLWinSts];
//    window4InvaildImageView.hidden = [self isVaild:_vehicleInfoModel.rRWinSts];
//
//
//    //车锁状态
//    switch ([self.vehicleInfoModel.drvDoorLock integerValue]) {
//        case NI_DOOR_LOCK_STS_CLOSED:
//            lockImageView.image = [UIImage imageNamed:@"Home_car1_lock_close"];
//            break;
//        case NI_DOOR_LOCK_STS_OPENED:
//            lockImageView.image = [UIImage imageNamed:@"Home_car1_lock_open"];
//            break;
//
//        default:
//            lockImageView.image = [UIImage imageNamed:@"Home_car1_lock_failure"];
//            break;
//    }
//    lockImageView.hidden = NO;
//
//    //设置车门
//    door1ImageView.hidden = [self isClosed:_vehicleInfoModel.drvDoorSts];
//    door2ImageView.hidden = [self isClosed:_vehicleInfoModel.passDoorSts];
//    door3ImageView.hidden = [self isClosed:_vehicleInfoModel.rLDoorSts];
//    door4ImageView.hidden = [self isClosed:_vehicleInfoModel.rRDoorSts];
//
//    door1InvaildImageView.hidden = [self isVaild:_vehicleInfoModel.drvDoorSts];
//    door2InvaildImageView.hidden = [self isVaild:_vehicleInfoModel.passDoorSts];
//    door3InvaildImageView.hidden = [self isVaild:_vehicleInfoModel.rLDoorSts];
//    door4InvaildImageView.hidden = [self isVaild:_vehicleInfoModel.rRDoorSts];
//
//    //车门打开时，对应车窗必须隐藏
//    if (!door1ImageView.hidden) {
//        if (!window1InvaildImageView.hidden) {
//            window1InvaildImageView.hidden = YES;
//            window1InvaildForDoorImageView.hidden = NO;
//        }
//        if (!window1ImageView.hidden) {
//            window1ImageView.hidden = YES;
//            window1ForDoorImageView.hidden = NO;
//        }
//    }
//    if (!door2ImageView.hidden) {
//        if (!window2InvaildImageView.hidden) {
//            window2InvaildImageView.hidden = YES;
//            window2InvaildForDoorImageView.hidden = NO;
//        }
//        if (!window2ImageView.hidden) {
//            window2ImageView.hidden = YES;
//            window2ForDoorImageView.hidden = NO;
//        }
//    }
//    if (!door3ImageView.hidden) {
//        if (!window3InvaildImageView.hidden) {
//            window3InvaildImageView.hidden = YES;
//            window3InvaildForDoorImageView.hidden = NO;
//        }
//        if (!window3ImageView.hidden) {
//            window3ImageView.hidden = YES;
//            window3ForDoorImageView.hidden = NO;
//        }
//    }
//    if (!door4ImageView.hidden) {
//        if (!window4InvaildImageView.hidden) {
//            window4InvaildImageView.hidden = YES;
//            window4InvaildForDoorImageView.hidden = NO;
//        }
//        if (!window4ImageView.hidden) {
//            window4ImageView.hidden = YES;
//            window4ForDoorImageView.hidden = NO;
//        }
//    }
//
//    trunkImageView.hidden = [self isClosed:_vehicleInfoModel.trunkSts];
//    trunkInvaildImageView.hidden = [self isVaild:_vehicleInfoModel.trunkSts];
}

- (BOOL)isClosed:(NSNumber *)number{
    if ([number integerValue] == 0 || [number integerValue] == -1 || [number integerValue] == -2) {
        return YES;
    }
    return NO;
}

- (BOOL)isVaild:(NSNumber *)number{
    if ([number integerValue] == -1) {
        return NO;
    }
    return YES;
}

//- (void)setVehicleInfoModel:(GWVehicleInfoModel *)vehicleInfoModel skyWindowSupport:(BOOL)support{
//    skyWindowSupport = support;
//    _vehicleInfoModel = vehicleInfoModel;
//    //如果当前没有有动画执行
//    if (runningAnimationType == NIVehicleAnimationTypeNone) {
//        [self reloadData];
//    }
//    //动画执行成功后会自动刷新车况
//}



#pragma mark - Animation
- (void)startAnimation:(NIVehicleAnimationType)type{
    //考虑当前有动画正在执行的情况
    if (runningAnimationType != NIVehicleAnimationTypeNone) {
        [self stopAnimatioin];
    }
    
    runningAnimationType = type;
    switch (type) {
        case NIVehicleAnimationTypeCloseWindow:
            animationTimes = 3;
            [self animationForCloseWindow];
            break;
        case NIVehicleAnimationTypeFlash:
            lightImageView.hidden = NO;
            honkImageView.hidden = NO;
            animationTimes = 3;
            [self animationForHonkAndFlash];
            break;
        case NIVehicleAnimationTypeLock:
            lockImageView.hidden = YES;
            lock1ImageView.hidden = NO;
            circleAnimationView.hidden = NO;
            [self animationForUnLock];
            break;
        case NIVehicleAnimationTypeUnlock:
            lockImageView.hidden = YES;
            unlock1ImageView.hidden = NO;
            circleAnimationView.hidden = NO;
            [self animationForLock];
            break;
        case NIVehicleAnimationTypeEngine:
            lockImageView.hidden = YES;
            startEngine1ImageView.hidden = NO;
            startEngine2ImageView.hidden = YES;
            circleAnimationView.hidden = NO;
            [self animationForLock];
            break;
            
        default:
            break;
    }
}

- (void)stopAnimatioin{
    [self stopLockUnlockAnimation];
}

- (void)animationForHonkAndFlash{
    lightImageView.alpha = 0.0f;
    honkImageView.alpha = 0.0f;
    
    [UIView animateWithDuration:1.0f animations:^{
        lightImageView.alpha = 1.0f;
        honkImageView.alpha = 1.0f;
    } completion:^(BOOL finished) {
        animationTimes--;
        if (animationTimes > 0) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self animationForHonkAndFlash];
            });
        }else{
            //更新至最新状态
            [self reloadData];
            runningAnimationType = NIVehicleAnimationTypeNone;
        }
    }];
    
}

- (void)animationForLock{
    [self lockUnlockAnimation];
}

- (void)animationForUnLock{
    [self lockUnlockAnimation];
}

- (void)setAlphaForWindowAnimation:(CGFloat)alpha{
    
    window1ImageView.hidden = YES;
    window2ImageView.hidden = YES;
    window3ImageView.hidden = YES;
    window4ImageView.hidden = YES;
    
    window1InvaildImageView.hidden = YES;
    window2InvaildImageView.hidden = YES;
    window3InvaildImageView.hidden = YES;
    window4InvaildImageView.hidden = YES;
    
    window1ForDoorImageView.hidden = YES;
    window2ForDoorImageView.hidden = YES;
    window3ForDoorImageView.hidden = YES;
    window4ForDoorImageView.hidden = YES;
    
    window1InvaildForDoorImageView.hidden = YES;
    window2InvaildForDoorImageView.hidden = YES;
    window3InvaildForDoorImageView.hidden = YES;
    window4InvaildForDoorImageView.hidden = YES;
    
    skyWindowImageView.hidden = YES;
    skyWindowInvaildImageView.hidden = YES;
    
    window1ForDoorAnimateImageView.hidden = door1ImageView.hidden;
    window1AnimateImageView.hidden = !door1ImageView.hidden;
    
    window2ForDoorAnimateImageView.hidden = door2ImageView.hidden;
    window2AnimateImageView.hidden = !door2ImageView.hidden;
    
    window3ForDoorAnimateImageView.hidden = door3ImageView.hidden;
    window3AnimateImageView.hidden = !door3ImageView.hidden;
    
    window4ForDoorAnimateImageView.hidden = door4ImageView.hidden;
    window4AnimateImageView.hidden = !door4ImageView.hidden;
    
    if (skyWindowSupport) {
        skyWindowAnimateImageView.hidden = NO;
    }
    
    NSArray *tmpArray = @[window1AnimateImageView,window2AnimateImageView,window3AnimateImageView,window4AnimateImageView,skyWindowAnimateImageView,window1ForDoorAnimateImageView,window2ForDoorAnimateImageView,window3ForDoorAnimateImageView,window4ForDoorAnimateImageView];
    
    for (UIView *view in tmpArray) {
        if (!view.hidden) {
            view.alpha = alpha;
        }
    }
}

- (void)animationForCloseWindow{
    [self setAlphaForWindowAnimation:0.0f];
    [UIView animateWithDuration:1.0f animations:^{
        [self setAlphaForWindowAnimation:1.0f];
    } completion:^(BOOL finished) {
        animationTimes--;
        if (animationTimes > 0) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self animationForCloseWindow];
            });
        }else{
            //更新至最新状态
            [self reloadData];
            runningAnimationType = NIVehicleAnimationTypeNone;
        }
    }];
}

- (void)lockUnlockAnimation{
    CGFloat rectWidth = circleAnimationView.frame.size.width;
    CGFloat lineWidth = 2.0f;
    CGFloat radius = (rectWidth - lineWidth)/2;
    
    circleAnimationlayer = [CALayer layer];
    circleAnimationlayer.frame = CGRectMake(0, 0, rectWidth, rectWidth);
    circleAnimationlayer.backgroundColor = [UIColor redColor].CGColor;
    
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(circleAnimationView.frame.size.width/2, circleAnimationView.frame.size.width/2) radius:radius startAngle:M_PI*3/2 endAngle:(M_PI*2 + M_PI*3/2)clockwise:YES];
    
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.strokeColor = [UIColor redColor].CGColor;
    shapeLayer.lineWidth = lineWidth;
    shapeLayer.strokeStart = 0;
    shapeLayer.strokeEnd = 0;
    shapeLayer.lineCap = @"round";
    shapeLayer.path = bezierPath.CGPath;
    
    [circleAnimationlayer setMask:shapeLayer];
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animation.duration = 2.0f;
    animation.delegate = self;
    animation.repeatCount = 1;
    animation.fromValue = [NSNumber numberWithFloat:0];
    animation.toValue = [NSNumber numberWithFloat:1];
    [shapeLayer addAnimation:animation forKey:@"strokeEndAnimation"];
    [circleAnimationView.layer addSublayer:circleAnimationlayer];
}

- (void)stopBaseAnimation{
    animationTimes = 0;
}

- (void)stopLockUnlockAnimation{
    if (circleAnimationlayer) {
        [circleAnimationlayer removeAllAnimations];
        [circleAnimationlayer removeFromSuperlayer];
        circleAnimationlayer = nil;
    }
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    [self stopLockUnlockAnimation];
    if (runningAnimationType == NIVehicleAnimationTypeEngine) {
        startEngine1ImageView.hidden = YES;
        startEngine2ImageView.hidden = NO;
        circleAnimationView.hidden = YES;
    }else if (runningAnimationType == NIVehicleAnimationTypeLock) {
        lock1ImageView.hidden = YES;
        lock2ImageView.hidden = NO;
        circleAnimationView.hidden = YES;
    }else{
        unlock1ImageView.hidden = YES;
        unlock2ImageView.hidden = NO;
        circleAnimationView.hidden = YES;
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self reloadData];
        runningAnimationType = NIVehicleAnimationTypeNone;
    });
}


@end
